package servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

@WebServlet(name = "FormServlet", urlPatterns = {"/form"})
public class FormServlet extends HttpServlet{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.println("<html>");
        writer.println("<body><h1>Servlet示例:订单信息提交页面</h1>");
        writer.println("<form method='post'>");
        writer.println("<table>");
        writer.println("<table>");
        writer.println("<tr>");
        writer.println("<td>姓名:</td>");
        writer.println("<td><input name='name' /></td>");
        writer.println("</tr>");
        writer.println("<tr>");
        writer.println("<td>地址:</td>");
        writer.println("<td><textarea name= 'address'" + "cols='40' rows='5'></textarea></td>");
        writer.println("</tr>");
        writer.println("<tr>");
        writer.println("<td>付款方式:</td>");
        writer.println("<td><select name='paymentMethod'>");
        writer.println(" <option>货到付款</option>");
        writer.println("<option>在线支付</option>");
        writer.println("</select></td>");
        writer.println("</tr>");
        writer.println("<tr>");
        writer.println("<td>店铺优惠:</td>");
        writer.println("<td><input type= 'radio' " + "name= 'discountMethod' " + "value= '店铺包邮'/>店铺包邮");
                writer. println("<input type='radio' " + "name= 'discountMethod'" + "value='不使用优惠'/>不使用优惠</td>");
                writer. println("</tr>");
        writer.println("<tr>");
        writer.println("<td>给卖家留言:</td>");
        writer.println("<td><textarea name= 'messageToSeller' " + "cols= '40' rows= '5'></textarea></td>");
        writer.println("</tr>");
        writer.println("<tr>");
        writer.println(" <td>&nbsp;</td>");
        writer.println("<td><textarea name= 'messageToSeller' "+"cols= '40' rows='5'></textarea></td>");
        writer.println("</tr>");
        writer.println("<tr>");
        writer.println("<td>开具发票:</td>");
        writer.println("<td><input type= 'checkbox' " + "name= 'invoice'/></td>");
        writer.println("</tr>");
        writer.println("<tr>");
        writer.println("<td>&nbsp;</td>");
        writer.println("<td><input type='reset'/>" + "<input type='submit'/></td>");
        writer.println("</tr>");
        writer.println("</table>");
        writer.println("</form>");
        writer.println("</body>");
        writer.println("</html>");

    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter writer = response.getWriter();
        writer.println("<html>");
        writer.println("<body><h1>Servlet示例:订单信息确认页面</h1>");
        writer.println("<table width= '500' border='1'>");
        writer.println("<tr>");
        writer.println("<td width='150'>姓名:</td>");
        writer.println("<td width='350'>" + request.getParameter("name" ) + "</td>");
        writer.println("</tr>");
        writer.println("<tr>");
        writer.println("<td>地址:</td>");
        writer.println("<td>" + request. getParameter( "address" ) + "</td>");
        writer.println("</tr>");
        writer.println("<tr>");
        writer.println("<td>付款方式:</td>");
        writer.println("<td>" + request. getParameter("paymentMethod" ) + "</td>");
        writer.println("</tr>");
        writer.println("<tr>");
        writer.println("<td>给卖家留言:</td>");
        writer.println("<td>");
        String[] messageToSellers = request.getParameterValues( "messageToSeller");
        if (messageToSellers != null) {
            for (String messageToSeller : messageToSellers) {
                writer. println(messageToSeller + "<br/>");
            }
        }
        writer.println("</td>");
        writer.println("</tr>");
        writer.println("<tr>");
        writer.println("<td>店铺优惠:</td>");
        writer.println("<td>" + request.getParameter("discountMethod") + "</td>");
        writer.println("</tr>");
        writer.println("<tr>");
        writer.println("<td>开具发票:</td>");
        writer.println("<td>");
        if (request. getParameter("invoice") == null) {
            writer.println("不开发票");
        } else {
            writer.println("开发票");
        }
        writer.println("</td>");
        writer.println("</tr>");
        writer.println("</table>");
        writer.println("<div style= 'border:1px solid #ddd;" + "margin-top:40px; font-size:90%'>");
        writer.println("Debug Info<br/>");
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String paramName = parameterNames.nextElement() ;
            writer.println( paramName + ": ");
            String[ ] paramValues = request.getParameterValues(paramName) ;
            for (String paramValue : paramValues) {
                writer.println(paramValue + "<br/>");
            }
            writer.println("</div>");
            writer.println("</body>");
            writer.println("</html>");
        }
    }

}
